package com.smart.yun;

import com.smart.enums.QrCodeTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 微信扫一扫跳转控制类
 */
@Controller
@RequestMapping("/mobile/wxapi")
public class ShortUrlRedirectController {
    private Logger log = LoggerFactory.getLogger(ShortUrlRedirectController.class);

    @Value("${admin.domainUrl}")
    private String domainUrl;

    @Value("${admin.appId}")
    private String appId;

    /**
     * 通用请求微信授权回调接口
     * <p>mobileaip是指与state中二维码类型对应的接口名称</p>
     *
     * @return
     * @see WxRedirectController
     */
    @RequestMapping(value = "/gowechat")
    public String gowechat(@RequestParam String state) {
        String[] params = state.split(",");
        String type = params[0];
        String mobilApi = "";
        //加油员扫码进入派券页面
        if (type.equals(QrCodeTypeEnum.STATION_CODE.getCode())) {
            log.info("加油员扫码进入派券页面，type = " + type);
            mobilApi = "/buswx/stationcp";
        }
        //用户扫派券码
        else if (type.equals(QrCodeTypeEnum.SEND_ECP_CODE.getCode())) {
            log.info("用户扫码进入领券页面，type = " + type);
            mobilApi = "/buswx/sendcp";
        }
        //用户扫门店核销码
        else if (type.equals(QrCodeTypeEnum.SHOP_USE_ECP_CODE.getCode())) {
            log.info("用户扫门店码核销，type = " + type);
            mobilApi = "/buswx/userusecp";
        }
        //门店扫用户核销码
        else if (type.equals(QrCodeTypeEnum.USER_USE_ECP_DETAIL_CODE.getCode())) {
            log.info("门店扫用户码核销，type = " + type);
            mobilApi = "/buswx/shopusecp";
        }
        //我的卡包
        else {
            log.info("我的卡包，type = " + type);
            mobilApi = "/buswx/home";
        }

        log.info("[shorturl.stationcp]appId=" + appId + ",state=" + state);
        String wxUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
                + appId + "&redirect_uri=" + domainUrl + mobilApi + "&response_type=code&scope=snsapi_base&state=" + appId +
                "," + state;
        log.info("gateway到微信拿code，跳转WxRedirect = " + wxUrl);
        return "redirect:" + wxUrl;
    }

    public String getDomainUrl() {
        return domainUrl;
    }

    public void setDomainUrl(String domainUrl) {
        this.domainUrl = domainUrl;
    }


    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }
}
